package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ShopInfoMapper;
import com.guigu.pojo.ShopImags;
import com.guigu.pojo.ShopInfo;
import com.guigu.service.ShopImagsService;
import com.guigu.service.ShopInfoService;
import com.guigu.service.ShopTypeInfoService;
import com.guigu.service.utils.MyIdAdd;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo>
        implements ShopInfoService {

    @Autowired
    ShopImagsService shopImagsService;

    @Autowired
    ShopInfoMapper shopInfoMapper;

    @Autowired
    ShopTypeInfoService shopTypeInfoService;

    @Autowired
    MyIdAdd myIdAdd;

    //添加商品
    @Override
    public Map addShop(ShopInfo shop, MultipartFile[] imgs, HttpServletRequest request) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "添加失败");
        shop.setShopNumber(myIdAdd.idadd("100", true));
        shop.setIsUp(0);
        shop.setShopUpnum(0);//上架数量
        shop.setIsGather(0); //未配置
        shop.setIsDelete(0);
        boolean b = this.save(shop);
        if (b) {
            //保存图片对象
            List<ShopImags> shopImagsList = new ArrayList<ShopImags>();
            //判断是否上传了图片
            for (MultipartFile img : imgs) {
                if (img.getSize() > 0) {
                    String apppath = request.getServletContext().getRealPath("/upload"); //路径名
                    File file = new File(apppath);
                    if (!file.exists()) {
                        //不存在就创建
                        file.mkdirs();
                    }
                    //获取文件名称
                    String fileName = img.getOriginalFilename();
                    //保存文件到路径
                    try {
                        img.transferTo(new File(apppath, fileName));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //将路径设置到对象
                    ShopImags shopImags = new ShopImags();
                    shopImags.setShopId(shop.getId());
                    shopImags.setImagUrl("upload/" + fileName);
                    shopImagsList.add(shopImags);
                }
            }
            //批量添加到图片表
            shopImagsService.saveBatch(shopImagsList);
            map.put("code", 1);
            map.put("msg", "添加成功");
        }

        return map;
    }

    //分页及所有
    @Override
    public Page<ShopInfo> queryAllShops(ShopInfo shop, Integer pageno, Integer pagesize) {
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<ShopInfo>();
        queryWrapper.eq("is_delete", 0);
        if (StringUtils.isNotBlank(shop.getShopName())) {
            queryWrapper.like("shop_name", shop.getShopName());
        }
        if (shop.getShopType()!=null){
            queryWrapper.eq("shop_type",shop.getShopType());
        }
        if (shop.getIsUp()!=null){
            queryWrapper.eq("is_up",shop.getIsUp());
        }
        Page<ShopInfo> page = this.page(new Page<ShopInfo>(pageno, pagesize), queryWrapper);
        for (ShopInfo shopInfo : page.getRecords()) {
            shopInfo.setType(shopTypeInfoService.getById(shopInfo.getShopType()));
            shopInfo.setImags(shopImagsService.queryImagsByShopId(shopInfo.getId()));
        }
        return page;
    }

    //获取商品编号
    @Override
    public List<String> getIds(String str) {
        List<String> list = new ArrayList<String>();
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<ShopInfo>();
        queryWrapper.likeRight("shop_number", str);
        List<ShopInfo> tempList = this.list(queryWrapper);
        for (ShopInfo obj : tempList) {
            list.add(obj.getShopNumber());
        }
        return list;
    }

    //修改商品上架下架
    @Override
    public Map isUp(ShopInfo shop) {
        Map map = new HashMap();
        map.put("code", 0);
        if (shop.getIsUp() == 0) {
            map.put("msg", "下架失败");
            shop.setShopUpnum(0);
            boolean b = this.updateById(shop);
            if (b) {
                map.put("code", 1);
                map.put("msg", "下架成功");
            }
        } else {
            //上架前检查商品库存是否上架商品数量 todo
            map.put("msg", "上架失败");
            boolean b = this.updateById(shop);
            if (b) {
                map.put("code", 1);
                map.put("msg", "上架成功");
            }
        }
        return map;
    }

    //修改商品信息
    @Override
    public Map editShopById(ShopInfo shop, MultipartFile[] files, String[] filenames, HttpServletRequest request) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "编辑失败");
        boolean b = this.updateById(shop);
        //将新的文件保存在服务器 并存入保存对象
        if(b) {
            QueryWrapper<ShopImags> queryWrapper=new QueryWrapper<ShopImags>();
            queryWrapper.eq("shop_id",shop.getId());
            shopImagsService.remove(queryWrapper);
            //删除该商品全部路径
            List<ShopImags> shopImagsList = new ArrayList<ShopImags>();
            for (MultipartFile img : files) {
                if (img.getSize() > 0) {
                    String apppath = request.getServletContext().getRealPath("/upload"); //路径名
                    File file = new File(apppath);
                    if (!file.exists()) {
                        //不存在就创建
                        file.mkdirs();
                    }
                    //获取文件名称
                    String fileName = img.getOriginalFilename();
                    //保存文件到路径
                    try {
                        img.transferTo(new File(apppath, fileName));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //将路径设置到对象
                    ShopImags shopImags = new ShopImags();
                    shopImags.setShopId(shop.getId());
                    shopImags.setImagUrl("upload/" + fileName);
                    shopImagsList.add(shopImags);
                }
            }
            //旧图片只保存文件名称
            if(filenames!=null && filenames.length>0) {
                for (String filename : filenames) {
                    ShopImags shopImags = new ShopImags();
                    shopImags.setShopId(shop.getId());
                    shopImags.setImagUrl(filename);
                    shopImagsList.add(shopImags);
                }
            }
            //批量添加到图片表
            shopImagsService.saveBatch(shopImagsList);
            map.put("code", 1);
            map.put("msg", "编辑成功");
        }
        return map;
    }

    //id查询商品
    @Override
    public ShopInfo queryShopById(Integer id) {
        ShopInfo shop = this.getById(id);
        //获取类型对象
        shop.setType(shopTypeInfoService.getById(shop.getShopType()));
        //获取图片
        shop.setImags(shopImagsService.queryImagsByShopId(shop.getId()));
        return shop;
    }

    //删除商品
    @Override
    public Map deleteShopById(ShopInfo shop) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg","删除失败");
        shop.setIsUp(0);
        shop.setShopUpnum(0);
        shop.setIsDelete(1);
        boolean b = this.updateById(shop);
        if (b){
            map.put("code", 1);
            map.put("msg","删除成功");
        }
        return map;
    }

}
